package geradatawarehouse.dao.postgres;

import geradatawarehouse.dao.postgres.abs.PostgresDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public abstract class AbstractAlunoPostgresDAO<T> extends PostgresDAO {

	protected final int anoSem;
	private final String clearAnoSemQuery;
	private boolean isCleaned = false;

	public AbstractAlunoPostgresDAO(final int anoSem,
			final String clearAnoSemQuery) throws IOException {
		this.anoSem = anoSem;
		this.clearAnoSemQuery = clearAnoSemQuery;
	}

	private void clearFromAnoSem() throws SQLException {
		try {
			final Statement smt = this.conn.createStatement();

			smt.executeUpdate(this.clearAnoSemQuery + this.anoSem);
		} catch (final SQLException e) {
			this.treatTransactionException();
			throw e;
		}
	}

	public void clearOnlyOnce() throws SQLException {
		if (!this.isCleaned) {
			this.clearFromAnoSem();
			this.isCleaned = true;
		}
	}

	public abstract void record(List<T> lista) throws SQLException;
}